package com.demo.paypalmanagement.common;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.demo.paypalmanagement.model.entity.*;
import com.demo.paypalmanagement.model.vo.*;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

public class BuildQuery {

    public static QueryWrapper<Order> buildOrderQuery(OrderVO vo){
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(StringUtils.isNotBlank(vo.getPayChannel()),Order::getPayChannel,vo.getPayChannel())
                .eq(ObjectUtils.isNotEmpty(vo.getPayOrderNo()),Order::getPayOrderNo, vo.getPayOrderNo())
                .eq(ObjectUtils.isNotEmpty(vo.getToken()),Order::getToken, vo.getToken())
                .eq(ObjectUtils.isNotEmpty(vo.getUserId()),Order::getUserId, vo.getUserId())
                .eq(ObjectUtils.isNotEmpty(vo.getStatus()),Order::getStatus, vo.getStatus())
                .between(ObjectUtils.isNotEmpty(vo.getBeginDate())&&ObjectUtils.isNotEmpty(vo.getEndDate()),Order::getCreateTime,vo.getBeginDate(),vo.getEndDate());
;
        return queryWrapper;
    }

    public static QueryWrapper<OrderItems> buildOrderItemQuery(OrderItemVO vo) {

        QueryWrapper<OrderItems> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(StringUtils.isNotBlank(vo.getProductName()),OrderItems::getProductName,vo.getProductName())
                .eq(ObjectUtils.isNotEmpty(vo.getOrderId()),OrderItems::getOrderId,vo.getOrderId())
                .eq(ObjectUtils.isNotEmpty(vo.getProductId()),OrderItems::getProductId, vo.getProductId())
                .between(ObjectUtils.isNotEmpty(vo.getBeginDate())&&ObjectUtils.isNotEmpty(vo.getEndDate()),OrderItems::getCreatedAt,vo.getBeginDate(),vo.getEndDate());

        ;
        return queryWrapper;

    }

    public static QueryWrapper<Products> buildProductItemQuery(ProductsVO vo) {

        QueryWrapper<Products> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(StringUtils.isNotBlank(vo.getName()),Products::getName,vo.getName())
                .eq(ObjectUtils.isNotEmpty(vo.getIsPrize()),Products::getIsPrize,vo.getIsPrize())
                .eq(ObjectUtils.isNotEmpty(vo.getIsActive()),Products::getIsActive, vo.getIsActive())
                .between(ObjectUtils.isNotEmpty(vo.getBeginDate())&&ObjectUtils.isNotEmpty(vo.getEndDate()),Products::getCreatedAt,vo.getBeginDate(),vo.getEndDate());
        ;
        return queryWrapper;

    }

    public static Wrapper<Message> buildMssageWrapper(MessageVO vo) {

        QueryWrapper<Message> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(StringUtils.isNotBlank(vo.getMessageType()), Message::getMessageType, vo.getMessageType())
                .eq(ObjectUtils.isNotEmpty(vo.getIsRead()), Message::getIsRead, vo.getIsRead())
                .eq(ObjectUtils.isNotEmpty(vo.getReceiverId()), Message::getReceiverId, vo.getReceiverId())
                .eq(ObjectUtils.isNotEmpty(vo.getSenderId()), Message::getSenderId, vo.getSenderId())
                .eq(ObjectUtils.isNotEmpty(vo.getIsRead()), Message::getIsRead, vo.getIsRead())
                .and(ObjectUtils.isNotEmpty(vo.getUserId()), wrapper ->
                        wrapper.eq(Message::getSenderId, vo.getUserId()).or().eq(Message::getReceiverId, vo.getUserId())
                );


        ;
        return queryWrapper;
    }

    public static QueryWrapper<Users> buildUserQuery(UserVO vo) {

        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(StringUtils.isNotBlank(vo.getAccount()),Users::getAccount,vo.getAccount())
                ;
        return queryWrapper;

    }
}
